﻿using Model;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using Utility.Helper;

namespace BLL
{
    public class QS_PLANWEEK_DETAIL_BLL : EntityBLL<QS_PLANWEEK_DETAIL>
    {
        public override bool Insert(Model.QS_PLANWEEK_DETAIL entity)
        {
            entity.N_WEEKDETAIL_ID = GetSequence("SEQ_WEEKDETAIL_ID");
            return base.Insert(entity);
        }

        public override bool Update(QS_PLANWEEK_DETAIL entity)
        {
            var sql = @"UPDATE QS_PLANWEEK_DETAIL
SET
	N_WEEKAPP_ID = {1},
	N_PROID = {2},
	C_PRONAME = '{3}',
	N_AMOUNT= {4},
	C_REMARK = '{5}',
	N_OPERATORID = {6},
	D_OPERATORTIME= TO_DATE('{7}','YYYY-MM-DD HH24:MI:SS'),
	N_BUSINESS_ID = {8},
	N_TYPE = {9},
	N_SYAMOUNT = {10},
	N_USEAMOUNT = {11},
	N_MONTHAMOUNT = {12},
	C_PROWEEKNUM = '{13}'
WHERE
	N_WEEKDETAIL_ID = {0}";
            sql = string.Format(sql,
                entity.N_WEEKDETAIL_ID,
                entity.N_WEEKAPP_ID,
                entity.N_PROID,
                entity.C_PRONAME,
                entity.N_AMOUNT,
                entity.C_REMARK,
                entity.N_OPERATORID,
                entity.D_OPERATORTIME.ToString("yyyy-MM-dd HH:mm:ss"),
                entity.N_BUSINESS_ID,
                entity.N_TYPE,
                entity.N_SYAMOUNT,
                entity.N_USEAMOUNT,
                entity.N_MONTHAMOUNT,
                entity.C_PROWEEKNUM);
            return NoQuery(sql) > 0 ? true : false;
        }

        public override QS_PLANWEEK_DETAIL Find(object key)
        {
            return Where("N_WEEKDETAIL_ID=" + key.ToString()).FirstOrDefault();
        }

        public QS_PLANWEEK_DETAIL Find(int applyId, int proId)
        {
            var dt = Where("N_WEEKAPP_ID=" + applyId + " AND N_PROID=" + proId);
            return dt.FirstOrDefault();
        }

        public List<QS_PLANWEEK_DETAIL> GetByApplyId(decimal id)
        {
            var sql = @"SELECT
                            T1.*
                        FROM QS_PLANWEEK_DETAIL T1
                        WHERE T1.N_WEEKAPP_ID = :APPLY_ID
                        ORDER BY T1.N_WEEKDETAIL_ID ASC";
            var parms = new List<Tuple<string, string, string>>();
            parms.Add(":APPLY_ID", id, "int");
            var entitys = ConvertEntity(Query(sql, parms));
            return entitys;
        }

        public JArray GetByApplyIdToJArray(decimal id)
        {
            var sql = @"SELECT
                            T1.*
                        FROM QS_PLANWEEK_DETAIL T1
                        WHERE T1.N_WEEKAPP_ID = :APPLY_ID
                        ORDER BY T1.N_WEEKDETAIL_ID ASC";
            var parms = new List<Tuple<string, string, string>>();
            parms.Add(":APPLY_ID", id, "int");
            var entitys = JArray.FromObject(Query(sql, parms));
            return entitys;
        }
    }
}